/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.utils;

import cn.ac.nercis.pes.community.constant.AppConstants;
import cn.ac.nercis.pes.community.constant.WindowMode;
import cn.ac.nercis.pes.community.controller.evaluation.EvaluationController;
import cn.ac.nercis.pes.community.model.vo.project.CurrentSystemVO;
import cn.ac.nercis.pes.community.support.event.CommonCallback;
import cn.ac.nercis.pes.community.views.evaluation.EvaluationView;
import javafx.application.HostServices;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * Gui信息存储类
 * @author zpy
 * @version 1.0.0
 */
public final class GuiStore {
    private static WindowMode model = WindowMode.NORMAL;
    private static Scene scene;
    private static Stage stage;
    private static StackPane root;
    private static String title;
    private static HostServices hostServices;
    private static BorderPane mainBody;

    public static WindowMode getModel(){
        return model;
    }

    public static void setModel(WindowMode model){
        GuiStore.model = model;
    }

    public static Scene getScene() {
        return scene;
    }

    public static void setScene(Scene scene) {
        GuiStore.scene = scene;
    }

    public static StackPane getRoot() {
        return root;
    }

    public static void setRoot(StackPane root) {
        GuiStore.root = root;
    }

    public static Stage getStage() {
        return stage;
    }

    public static void setStage(Stage stage) {
        GuiStore.stage = stage;
    }

    public static String getTitle() {
        return title;
    }

    public static void setTitle(String title) {
        GuiStore.title = title;
    }

    public static HostServices getHostServices() {
        return hostServices;
    }

    public static void setMainWindowBody(BorderPane mainWindowBody){
        mainBody = mainWindowBody;
    }

    public static void setMainBodyContent(@NonNull Node node){
        mainBody.setCenter(node);
    }

    public static void setHostServices(HostServices hostServices) {
        GuiStore.hostServices = hostServices;
    }

    public static void applyQuickSave(@NonNull Node page, CommonCallback saveMethod){
        page.setOnKeyPressed(keyEvent->{
            if(keyEvent.isShortcutDown() && keyEvent.getCode() == KeyCode.S){
                if(Objects.nonNull(saveMethod)){
                    saveMethod.action();
                }
            }
        });
    }

    public static void enterSystemEvaluation(@NonNull CurrentSystemVO system){
        enterSystemEvaluation(system, StringUtils.EMPTY);
    }

    public static void enterSystemEvaluation(@NonNull CurrentSystemVO system,String model){
        var oldSystem = AppStore.getCurrentSystem(system.getSystemId());
        if(Objects.nonNull(oldSystem)){
            if(oldSystem.getEvaluationStage().isIconified()){
                oldSystem.getEvaluationStage().setIconified(false);
            }
            oldSystem.getEvaluationStage().show();
            oldSystem.getEvaluationStage().requestFocus();
            return;
        }
        AppStore.addCurrentSystem(system);
        var evaluationStage = new Stage();
        evaluationStage.initStyle(StageStyle.TRANSPARENT);
        evaluationStage.getIcons().addAll(getStage().getIcons());
        evaluationStage.setTitle(ControlUtils.getEvaluationWindowTitle(system.getSystemName()));
        system.setEvaluationStage(evaluationStage);
        var evaluationView = Objects.requireNonNull(AppStore.getEvaluationView(system.getSystemId(), EvaluationView.class));
        var evaluationScene = new Scene(evaluationView.load(), Color.TRANSPARENT);
        system.setEvaluationScene(evaluationScene);
        evaluationStage.setScene(evaluationScene);
        if(evaluationView.getController() instanceof EvaluationController controller){
            controller.initData(system.getSystemId(),model);
        }
        evaluationStage.show();

    }

    public static List<Image> readIcons(){
        return List.of(new Image(Objects.requireNonNull(SpringUtils.class.getResource(AppConstants.APP_ICON)).toExternalForm()));
    }
}